package app.model.flare.vos
{
	import flash.events.EventDispatcher;
	
	public class ActionVO extends EventDispatcher  implements IEncodeable, IDecodeable
	{
		public static const ACTION_PREFIX:String = "action";
		public static const CASE_DELIMITER:String = " ";
		public static const CAPS_DELIMITER:String = "_";
		

		[Bindable]
		public var label:String;

		
		public var _name:String;
		private var _target:StateVO;
		private var _source:StateVO;
		private var _targetName:String;
	
			
		public function ActionVO(  )
		{
			
			
		}
		[Bindable]
		public function get name():String{
			return _name;
		}
		
		public function set name (val:String):void{
			_name = val;
			setLable();
		}
		
		private function setLable():void{
			this.label = name//+" => "+_target.name;
		}
		
		public function get target():StateVO{
			return _target;
		}
		
		public function set target(state:StateVO):void{
			_target = state;
		}
		public function get source():StateVO{
			return _source;
		}
		
		public function encode(o:Object):void{
			this.name = o.name;
			_target = o.target as StateVO;
			if(!_target)_targetName = String( o.target );
		}
		
		public function decode():Object{
			return {	name:this.name,
						target:target.fullPath
						};
		}
		
		public function toXML():XML{
			return <transition	action={slug} 
                   					target={target.nameSlug}
                   		/>
		}
		
		public function toXMLString():String{
			return "\t\t\t<transition action={" + capsSlug + "} target={" + target.nameCapsSlug + "}/>\r\t";
		}
		
		
		
	
		
		public function get slug():String{
			var split:Array = name.toLowerCase().split(CASE_DELIMITER),
				n:int = 1,
				s:String,
				p:String;
			//making the first character a cap for camel case
			while( n < split.length ){
				p = String(split[n])
				s = p.charAt().toUpperCase();
				split[n] = s + p.slice(1);
				n++;
			}			
			return ACTION_PREFIX + StateVO.NAME_DELIMITER + split.join("");
		} 
		
		public function get capsSlug():String{
			var split:Array = name.toUpperCase().split(CASE_DELIMITER);			
			return ACTION_PREFIX.toUpperCase() + CAPS_DELIMITER +split.join(CAPS_DELIMITER);
		} 
		
		public function inseminate(states:Array):void{
			var state:StateVO,
				find:int,
				t:Boolean,
				s:Boolean;
			if(!_targetName ||  _target &&  _source)return;
			for each (state in states) {
				if(state.fullPath == _targetName){
					_target = state;
					setLable();
					t = true
				}
				if(state.contains( name )){
					_source = state;
					s = true
				}
				if(t&&s)return;
			}
		}
		
			

	}
} 